package com.gxar.timeline.server.base.model.hmd.mapper;

import com.gxar.timeline.server.base.common.enums.CurrencyEnum;
import com.gxar.timeline.server.base.model.hmd.vo.BaseModelVO;
import com.gxar.timeline.server.base.model.hmd.vo.ModelVO;
import com.gxar.timeline.server.db.model.HmdProductModelEntity;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author Linsy
 * @version 1.0
 * @created 2023/9/22 10:45 AM
 */
@Mapper
public interface HmdProductModelMapStruct {
    HmdProductModelMapStruct INSTANCE = Mappers.getMapper(HmdProductModelMapStruct.class);

    /**
     * 实体转视图
     *
     * @param entity 实体
     * @return 视图
     */
    @Mapping(target = "symbol", expression = "java(com.gxar.timeline.server.base.common.enums.CurrencyEnum.getBySymbol(entity.getSymbol()).orElse(com.gxar.timeline.server.base.common.enums.CurrencyEnum.CNY).getSign())")
    @Mapping(target = "id", expression = "java(java.lang.String.valueOf(entity.getId()))")
    ModelVO entityToVo(HmdProductModelEntity entity);

    /**
     * 实体转视图
     *
     * @param entities 实体
     * @return 视图
     */
    List<ModelVO> entitiesToVo(List<HmdProductModelEntity> entities);


    @Mapping(target = "symbol", expression = "java(convertCurrency(vo.getSymbol()))")
    HmdProductModelEntity baseToEntity(BaseModelVO vo);

    List<HmdProductModelEntity> basesToEntities(List<BaseModelVO> vo);

    default String convertCurrency(String symbol) {
        Optional<CurrencyEnum> currencyEnum = CurrencyEnum.getBySymbol(symbol);
        return currencyEnum.map(CurrencyEnum::getSymbol).orElse(null);
    }

}
